import Vue from "vue";
import "element-ui/lib/theme-chalk/index.css";
import "@/assets/element-ui.css"; //重写 element组件样式
import ElementUI from "element-ui"; //使用 element组件库
import appConfig from "@/config/appConfig";

Vue.component("mf-versioning", () => import("@/components/Versioning.vue"));
Vue.component("mf-monacoEditor", () => import("@/components/MonacoEditor.vue"));
Vue.component("mf-monacoComparer", () => import("@/components/MonacoComparer.vue"));
Vue.component("mf-fileUpload", () => import("@/components/FileUpload.vue"));
Vue.component("mf-typeSelect", () => import("@/components/TypeSelect.vue"));
Vue.component("mf-selectFile", () => import("@/components/SelectFile.vue"));
Vue.component("mf-exporter", () => import("@/components/Exporter.vue"));
Vue.component("mf-importer", () => import("@/components/Importer.vue"));

Vue.use(ElementUI, appConfig.eleOptions);

window.alert = (msg, title, icon, callback) => {
  ElementUI.MessageBox({
    title: title,
    message: msg,
    type: icon || "info",
    callback: callback,
    confirmButtonText: "确定",
  });
};

window.confirm = (msg, title, icon, acceptCallback, cancelCallback) => {
  ElementUI.MessageBox({
    title: title,
    message: msg,
    showCancelButton: true,
    type: icon || "warning",
    closeOnClickModal: false,
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    callback(action, instance) {
      switch (action) {
        case "confirm":
          if (acceptCallback && typeof acceptCallback == "function")
            acceptCallback(instance);
          break;
        case "cancel":
        default:
          if (cancelCallback && typeof cancelCallback == "function")
            cancelCallback(instance);
          break;
      }
    },
  });
};

window.success = (msg, callback) => {
  window.alert(msg, "提示", "success", callback);
};

window.info = (msg, callback) => {
  window.alert(msg, "提示", "info", callback);
};

window.warning = (msg, callback) => {
  window.alert(msg, "提示", "warning", callback);
};

window.fail = (msg, callback) => {
  window.alert(msg, "错误", "error", callback);
};

window.prompt = (msg, title, icon, callback, onError) => {
  ElementUI.MessageBox({
    title: title,
    message: msg,
    showInput: true,
    showCancelButton: true,
    type: icon,
    closeOnClickModal: false,
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  })
    .then((e) => {
      if (callback && typeof callback == "function") callback(e.value);
    })
    .catch(() => {
      if (onError && typeof onError == "function") onError();
    });
};
